package com.muje.android.toiletcard;

import android.content.Context;
import android.opengl.Visibility;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PlayerControl extends LinearLayout {

	private Poker poker;
	private Player player;

	protected Button historyButton;
	protected Button toiletButton;
	protected Button crossButton;
	protected Button skipButton;
	protected Button leaveButton;
	protected Button playerButton;
	protected TextView bonusTextView;
	protected ImageView thumbImage;
	protected TextView playerTextView;

	protected LinearLayout playerMenuLayout;

	public PlayerControl(Context context) {
		super(context);
	}

	public PlayerControl(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout compoundView = (LinearLayout) inflater.inflate(R.layout.player_control, this);

		playerMenuLayout = (LinearLayout) compoundView.findViewById(R.id.playerMenuLayout);

		historyButton = (Button) compoundView.findViewById(R.id.historyButton);
		historyButton.setOnClickListener(historyOnClick);

		toiletButton = (Button) compoundView.findViewById(R.id.toiletButton);
		toiletButton.setOnClickListener(toiletOnClick);

		crossButton = (Button) compoundView.findViewById(R.id.crossButton);
		crossButton.setOnClickListener(cancelOnClick);

		skipButton = (Button) compoundView.findViewById(R.id.skipButton);
		skipButton.setOnClickListener(skipOnClick);

		leaveButton = (Button) compoundView.findViewById(R.id.leaveButton);
		leaveButton.setOnClickListener(leaveOnClick);

		playerButton = (Button) compoundView.findViewById(R.id.playerButton);
		playerButton.setOnClickListener(playerOnClick);

		thumbImage = (ImageView) compoundView.findViewById(R.id.thumbImage);
		playerTextView = (TextView) compoundView.findViewById(R.id.playerTextView);
		bonusTextView = (TextView) compoundView.findViewById(R.id.bonusTextView);

		// hide menu when first initial
		playerMenuLayout.setVisibility(INVISIBLE);
	}

	protected OnClickListener historyOnClick = new OnClickListener() {

		public void onClick(View v) {
			// TODO: show history
		}
	};
	protected OnClickListener toiletOnClick = new OnClickListener() {

		public void onClick(View v) {
			if (player != null) {
				player.goToilet();
				bonusTextView.setText(getBonusCard());
			}
		}
	};
	protected OnClickListener cancelOnClick = new OnClickListener() {

		public void onClick(View v) {
			if (player != null) {
				player.cancelPunishment();
				bonusTextView.setText(getBonusCard());
			}
		}
	};
	protected OnClickListener skipOnClick = new OnClickListener() {

		public void onClick(View v) {
			poker.skip();
		}
	};
	protected OnClickListener leaveOnClick = new OnClickListener() {

		public void onClick(View v) {
			poker.removePlayer(player);
		}
	};
	protected OnClickListener playerOnClick = new OnClickListener() {

		public void onClick(View v) {

			if (playerMenuLayout.getVisibility() == VISIBLE)
				playerMenuLayout.setVisibility(INVISIBLE);
			else
				playerMenuLayout.setVisibility(VISIBLE);
		}
	};

	public void setPoker(Poker poker) {
		this.poker = poker;
	}

	public void updatePlayer(Player player) {
		
		//if(player == null) return;

		this.player = player;
		playerTextView.setText(this.player.Name);

		bonusTextView.setText(getBonusCard());
		thumbImage.setVisibility(isThumbsup() ? VISIBLE:INVISIBLE);
		toiletButton.setVisibility(player.hasToiletKey() ? VISIBLE : GONE);
	}

	private boolean isThumbsup() {
		
		for (Card card : player.Cards) {

			if (card.Text.contains(Poker.THUMBS_UP_NUMBER))
				return true;
		}

		return false;
	}

	private String getBonusCard() {

		String result = "";
		for (Card card : player.Cards) {

			// if (number == "7" || number == "10" || number == "Q" || number == "K") {
			if (card.Text.contains(Poker.TOILET_NUMBER)
					|| card.Text.contains(Poker.SAFETY_KEY_NUMBER)
					|| card.Text.contains(Poker.THUMBS_UP_NUMBER)
					|| card.Text.contains(Poker.COMMANDER_NUMBER)) {

				String number = card.Text.substring(0, 2).trim();
				result += number + " ";
			}
		}

		result = result.trim();
		return result;
	}
}
